package worklog.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import worklog.db.ProjectChartDB;
import worklog.dto.ResponseDTO;

@WebServlet("/projectActivity")
public class ProjectActivity extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ProjectChartDB projectDB;
	private ResponseDTO responder = new ResponseDTO();
	
    public ProjectActivity () throws Exception{ super(); }
    
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		//doGet(request, response); 
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
		projectDB = new ProjectChartDB();
		PrintWriter out = response.getWriter();
		response.setContentType("application/json");
    	if (projectDB.isReady()) out.print(processRequest(request));
    	else out.print(responder.showError("DB connection error"));
    	projectDB.close();
    	out.close();
	}
	
	private String processRequest(HttpServletRequest request){
    	String projectIdStr = request.getParameter("projectId");
    	int projectId = -1;
    	if (!projectIdStr.isEmpty()) projectId = Integer.parseInt(projectIdStr);
    	else return responder.showError("Incorrect parameters");
    	
    	String project = "";
    	try { project = projectDB.projectActivity(projectId).toString(); } catch (SQLException e) {}
    	
    	return project;
	}
}